﻿namespace WLFramework.Web.Binder.ValueProvider
{
    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Linq;
    using System.Text;
    using System.Web;

    public static class DefaultValueProviderFactory
    {
        private static readonly List<string> defaultProviderNames;

        static DefaultValueProviderFactory()
        {
            defaultProviderNames = new List<string>() 
            {
                "FormValueProvider", // 优先级高
                "QueryStringValueProvider",
                "SessionValueProvider",
                "CookieValueProvider",  // 低
            };
        }

        public static IValueProvider CreateInstance(string fullname, params object[] args)
        {
            return (IValueProvider)Activator.CreateInstance(Type.GetType(fullname), args);
        }

        public static IValueProvider CreateDefaultProvider(string provider, HttpContext context)
        {
            Shield.AssertNotNull(ref provider, "provider");

            switch (provider)
            {
                case "FormValueProvider":
                    return new FormValueProvider(context.Request.Form);

                case "QueryStringValueProvider":
                    return new QueryStringValueProvider(context.Request.QueryString);

                case "SessionValueProvider":
                    return new SessionValueProvider(context.Session);

                case "CookieValueProvider":
                    return new CookieValueProvider(context.Request.Cookies);

                default:
                    throw new ArgumentException("provider");
            }
        }

#if FW4
        public static IValueProvider CreateDefaultProvider(string provider, HttpContextBase context)
        {
            Shield.AssertNotNull(ref provider, "provider");

            switch (provider)
            {
                case "FormValueProvider":
                    return new FormValueProvider(context.Request.Form);

                case "QueryStringValueProvider":
                    return new QueryStringValueProvider(context.Request.QueryString);

                case "SessionValueProvider":
                    return new SessionBaseValueProvider(context.Session);

                case "CookieValueProvider":
                    return new CookieValueProvider(context.Request.Cookies);

                default:
                    throw new ArgumentException("provider");
            }
        }
#endif

        public static IEnumerable<IValueProvider> GetDefaultProviders(HttpContext context)
        {
            return from p in defaultProviderNames
                   select CreateDefaultProvider(p, context);
        }

#if FW4
        public static IEnumerable<IValueProvider> GetDefaultProviders(HttpContextBase context)
        {
            return from p in defaultProviderNames
                   select CreateDefaultProvider(p, context);
        }
#endif
    }
}
